//
//  HMUserAccount.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/9/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMUserAccount: NSObject, NSCoding {
    
    /// 访问令牌
    var access_token: String?
    /// 生命周期，多少秒之后就accessToken就不能使用了
    var expires_in: TimeInterval = 0 {
        didSet{
            // 过期时间 ＝ 当前时间 + expires_in
            expiresDate = Date(timeIntervalSinceNow: expires_in)
        }
    }
    /// 当前用户的id
    var uid: String?
    /// 过期日期
    var expiresDate: Date?
    /// 用户的昵称
    var name: String?
    /// 用户的头像地址
    var profile_image_url: String?
    
    init(dict: [String: Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    // MARK: - NSCoding
    
    // 解档
    required init?(coder decoder: NSCoder) {
        access_token = decoder.decodeObject(forKey: "access_token") as? String
        uid = decoder.decodeObject(forKey: "uid") as? String
        expiresDate = decoder.decodeObject(forKey: "expiresDate") as? Date
        name = decoder.decodeObject(forKey: "name") as? String
        profile_image_url = decoder.decodeObject(forKey: "profile_image_url") as? String
    }
    
    // 归档
    func encode(with encoder: NSCoder) {
        encoder.encode(access_token, forKey: "access_token")
        encoder.encode(uid, forKey: "uid")
        encoder.encode(expiresDate, forKey: "expiresDate")
        encoder.encode(name, forKey: "name")
        encoder.encode(profile_image_url, forKey: "profile_image_url")
        
    }
}
